package com.damei.hospital_android.view;

import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.damei.hospital_android.R;
import com.damei.hospital_android.model.ItemModel;
import com.xuexiang.xui.widget.layout.XUILinearLayout;

import java.util.List;

public class GridAdapter extends BaseAdapter {

    private List<ItemModel> mData;//定义数据。
    private LayoutInflater mInflater;
    private int wmWidth;

    public GridAdapter(LayoutInflater inflater, List<ItemModel> data, int width) {
        mData = data;
        mInflater = inflater;
        wmWidth = width;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = mInflater.inflate(R.layout.grid_item_layout, null);

        LinearLayout layout = view.findViewById(R.id.card_layout);
        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)layout.getLayoutParams();
        lp.width = (wmWidth-40)/4-16;
        lp.height = (wmWidth-40)/4;
        layout.setLayoutParams(lp);

        ItemModel itemModel = mData.get(position);
        TextView nameText = view.findViewById(R.id.name_text);
        ImageView iconImg = view.findViewById(R.id.icon_img);
        nameText.setText(itemModel.getModuleName());
        Glide.with(view).load(itemModel.getModuleUrl()).into(iconImg);

        return view;
    }
}
